package com.example.demo.utils;

import com.example.demo.entity.Menu;
import io.netty.util.internal.StringUtil;

import java.util.ArrayList;
import java.util.List;

public class MenuUtil {
    public static List<Menu> generateMenu(List<Menu> resourceList){
        List<Menu> menuList=new ArrayList<>();
        for (Menu childrenDynamicMenu:resourceList){
            //只要不是顶级目录都要寻找其父目录
            if(!childrenDynamicMenu.getUpperId().toString().equals("0")){
                //把其添加到其父目录里
                for (Menu parentDynamicMenu:resourceList){
                    //找到其父级目录
                    if(childrenDynamicMenu.getUpperId().equals(parentDynamicMenu.getMenuId())){
                        List<Menu> children = parentDynamicMenu.getChildren();
                        if(children==null){
                            children=new ArrayList<Menu>();
                        }
                        children.add(childrenDynamicMenu);
                        parentDynamicMenu.setChildren(children);
                    }
                }
            }
        }
        //只返回父级菜单即可
        for (Menu dynamicMenuVO:resourceList){
            if(dynamicMenuVO.getUpperId().equals("0")){
                menuList.add(dynamicMenuVO);
            }
        }
        return menuList;
    }

    public static List<Menu> getMenus(List<Menu> list){
        list.forEach(it->{
            it.setChildren(getMenus1(it.getChildren(),it.getPath()));
        });
        return list;
    }
    public static List<Menu> getMenus1(List<Menu> list,String parentPath){
        if(list!=null &&list.size()>0){
           list.forEach(it->{
               it.setPath(parentPath+"/"+it.getPath());
               it.setChildren(getMenus1(it.getChildren(),it.getPath()));
           });
        }
        return list;
    }
}

